package com.bc.web.controller.merch;


import java.awt.Color;
import java.io.File;
import java.io.IOException;

import com.bc.web.config.R;
import com.bc.web.config.SystemEnum;
import com.bc.web.controller.PictureProcess;
import com.bc.web.model.Merch;
import com.bc.web.model.MerchUser;
import com.bc.web.service.ReportService;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSFile;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

public class ReportController extends FSController{
	
	public void index() {
		render("order/orderReport.html");
	}

	public void download() {
		MerchUser merchUser = (MerchUser) getSession().getAttribute("merchUser");
		String startSettleDate = getPara("startSettleDate");
		String endSettleDate = getPara("endSettleDate");
		String reportType = getPara("reportType");
		if ((StrKit.isBlank(startSettleDate) || StrKit.isBlank(endSettleDate))) {
			renderFail("起止与结束日切日期必填！");
			return;
		}
		ReportService reportService = new ReportService();
		File file = null;
		if ("localDetail".equals(reportType)) {
			file = reportService.localDetail(merchUser.getMerchCode(), startSettleDate, endSettleDate);
		} 
		if (file == null) {
			renderFail("没有满足条件的数据");
		} else {
			renderFile(file);
		}
	}
	
	private static final String REPORT_PATH = PathKit.getWebRootPath()+File.separator +"payimg"+File.separator; //SystemEnum.FilePath.ReportTmp.getValue();
	
	public void viewQcode() throws IOException{
//		
//		System.out.println(
//		PathKit.getWebRootPath() + ",------\n" + PathKit.getRootClassPath()
//		);
		MerchUser merchUser = (MerchUser) getSession().getAttribute("merchUser");
		Merch merch = Merch.dao.selectByMerchCode(merchUser.getMerchCode());
		
		File file = null;
		//String targerPath = "";
		String targetPath = REPORT_PATH + merchUser.getMerchCode() + ".jpg";
		String logoPath = REPORT_PATH + "logook.png";
		String srcImgPath =REPORT_PATH + "backgroup.jpg";
		file = new File(targetPath);
		if (file == null || !file.exists() || !file.isFile()) {
			PictureProcess.CreateQrcodeToImgPath(R.QCodeUrl+merchUser.getMerchCode(), targetPath,logoPath,16,16,false);
			// 二维码当水印添加到图片
			PictureProcess.markImageByIcon(targetPath, srcImgPath, targetPath, 1 , 165);
			PictureProcess.createStringMark(targetPath, merch.getName(), Color.WHITE, 70, targetPath, 1800, 600);
			file = new File(targetPath);
			
		}
		this.setAttr("merch", merchUser.getMerchCode());
		this.setAttr("org", merchUser.getOrgCode());
		render("order/viewQcode.html");
	}
	
	
	@SuppressWarnings("unused")
	public void downloadQcodeJpg() {
		MerchUser merchUser = (MerchUser) getSession().getAttribute("merchUser");
		File file = null;
		String targetPath = REPORT_PATH + merchUser.getMerchCode() + ".jpg";
		String logoPath = REPORT_PATH + "logook.png";
		file = new File(targetPath);
		if (file == null || !file.exists() || !file.isFile()) {
			renderFail("没有满足条件的数据");
		} else {
			renderFile(file);
		}
	}
	
	
	
}
